<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-10-31
 * Time: 15:40
 */

namespace app\web\controller;


use app\src\base\helper\ConfigHelper;
use app\src\base\helper\ValidateHelper;
use app\src\base\utils\CacheUtils;
use app\src\customer\logic\CustomerInfoLogic;
use app\src\customer\model\CustomerInfo;
use app\src\user\facade\DefaultUserFacade;
use think\Controller;

class Alibaichuan extends Controller
{
    /**
     * 客服列表页面
     * @return mixed
     */
    public function im_list(){
        $uid = $this->request->get('uid',0);
        $userInfo = $this->getInfo($uid);
        $this->assign("user",$userInfo);
        $map = ['to_uid'=>$uid];
        $page = ['curpage'=>1,'size'=>50];
        $result = (new CustomerInfoLogic())->queryDetail($map,$page,"update_time desc");
        $list = $result['info']['list'];
        $tmp = [];
        foreach ($list as $item){
            if(method_exists($item,"toArray")){
                $item['time'] = date('Y-m-d H:i:s', $item['update_time']);
                array_push($tmp,$item->toArray());
            }
        }
        $this->assign("user_list",$tmp);
        $this->assign('uid',$uid);
        return $this->fetch();
    }

    public function contact_list(){
        $uid = $this->request->get('uid',0);
        $map = ['to_uid'=>$uid];
        $page = ['curpage'=>1,'size'=>50];
        $result = (new CustomerInfoLogic())->queryDetail($map,$page,"update_time desc");
        if($result['status']){
            foreach($result['info']['list'] as &$value){
                $value['head'] = ConfigHelper::getAvatarUrl($value['uid'],120);
                $value['time'] = date('Y-m-d H:i:s', $value['update_time']);
            }
            $this->result($result['info'],0,'',"json");
        }else{
            $this->result("",-1,$result['info'],"json");
        }
    }

    public function im(){
        $welcomeMsg = CacheUtils::getConfig("alibaichuan_welcome_msg");

        $uid = $this->request->get('uid',0);
        $info = $this->getInfo($uid);
        $touid = $this->request->get('touid',0);

        $toUserInfo = $this->getInfo($touid);
        $this->assign("welcome_msg",$welcomeMsg);
        $this->assign("touser",$toUserInfo);
        $this->assign("user",$info);

        return $this->fetch();
    }

    private function getInfo($uid){
        $logic = new DefaultUserFacade();
        $result = $logic->getInfo($uid);
        if(!ValidateHelper::legalArrayResult($result)){
            $this->error('非法uid'.$uid);
        }

        $uid = $result['info']['id'];
        $openim_uid = $result['info']['alibaichuan_id'];
        $nickname = $result['info']['nickname'];
        $password = $result['info']['password'];
        if(empty($openim_uid)){
            $result = (new DefaultUserFacade())->updateBaichuanUser($uid,$password,$nickname);
            if($result['status']){
                $openim_uid = $result['info'];
            }
        }
        $user = [
            'avatar'=>ConfigHelper::getAvatarUrl($uid,120).'&t='.time(),
            'uid'=>$openim_uid,
            'password'=>$password,
            'title'=>'正在对话中..',
        ];
        return $user;
    }

}